package com.zshan.clinic.mini.controller;


import com.zshan.clinic.common.entity.HeaderDTO;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.mini.annotation.Permission;
import com.zshan.clinic.mini.entity.UserInfo;
import com.zshan.clinic.mini.entity.WechatLoginRequest;
import com.zshan.clinic.mini.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 登录
 */
@RestController
@RequestMapping("/login")
public class LoginController extends AbstractController{

    @Autowired
    private LoginService loginService;

    /**
     * 小程序一键登录
     * @param
     * @return
     */
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    @Permission
    public RestResp<UserInfo> loginByWechat(@RequestBody @Valid WechatLoginRequest loginRequest) {
        HeaderDTO headerDTO = getHeader();
        UserInfo userInfo = loginService.loginByWechat(loginRequest,headerDTO.getClinicId());
        return RestResp.SUCCESS(userInfo);
    }

    /**
     * 退出登陆
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @Permission
    public RestResp<Void> logout() {
        HeaderDTO headerDTO = getHeader();
        loginService.logout(headerDTO.getToken());
        return RestResp.SUCCESS();
    }
}
